<?php

function getConnect($config)
{
    $dbConn = mysql_connect(
        $config["host"],
        $config["username"],
        $config["password"],
        $config["db"]
    ) or die("Không thể kết nối database");
    mysql_select_db($config["db"]) or die ("Không tồn tại db : " . $config["db"]);
    mysql_query("SET charactor_set_results=utf8",$dbConn);
    mysql_query("SET NAMES 'utf8'");
    return $dbConn;
}

function query($query = null){
    global $conn;
    $rlData = null;
    if(isset($conn) && $conn != null && $query != null) {
        $rlData = mysql_query($query);
    }
    return $rlData;
}

function insert($table, $arrData)
{
    $rlData = null;
    if($table != null && count($arrData) > 0){
        $colQuery = "(".implode(',', array_keys($arrData)).")";
        $valQuery = "('".implode("','", array_values($arrData))."')";
        $sqlQuery = "INSERT INTO ".$table.$colQuery." VALUES ".$valQuery;
        $rlData = query($sqlQuery);
    }
    return $rlData;
}

function deleteById($table, $id)
{
    $rlData = null;
    if($table != null && $id != null){
        $id = trim(strip_tags($id));
        $table = trim(strip_tags($table));
        $sqlQuery = "DELETE  FROM ".$table." WHERE id = ".$id;
        $rlData = query($sqlQuery);
    }
    return $rlData;
}

function delete($table, $where = "1=1")
{
    $rlData = null;
    if($table != null){
        $table = trim(strip_tags($table));
        $sqlQuery = "DELETE  FROM ".$table." WHERE 1=1 and ".$where;
        $rlData = query($sqlQuery);
    }
    return $rlData;
}

function update($table, $arrData, $where = "1=1")
{
    $rlData = null;
    $count = count($arrData);
    if($table != null && $count > 0){
        $sqlQuery = "UPDATE ".$table." SET ";
        $i = 0;
        foreach($arrData as $col => $val) {
            $i++;
            $sqlQuery .= $col."='".$val."'";
            if($i < $count)
                $sqlQuery .= ", ";
        }
        $sqlQuery .= " where 1 = 1 and ".$where;
        $rlData = query($sqlQuery);
    }
    return $rlData;
}

function findById($table = null, $id = null, $arrCol = "*")
{
    $rlData = null;
    if($table != null && $id != null){
        $id = trim(strip_tags($id));
        $table = trim(strip_tags($table));
        $sqlQuery = "SELECT ".$arrCol." FROM ".$table." WHERE id = ".$id;
        $rlData = query($sqlQuery);
        if($rlData != null)
            $rlData = mysql_fetch_array($rlData);
    }
    return $rlData;
}

function findOne($table, $where = '1=1', $arrCol = "*")
{
    $rlData = null;
    if($table != null){
        $table = trim(strip_tags($table));
        $sqlQuery = "SELECT ".$arrCol." FROM ".$table." WHERE 1=1 and ".$where;
        $query = query($sqlQuery);
        $rlData = array();
        if($query != null) {
            $rlData = mysql_fetch_array($query);
        }

    }
    return $rlData;
}

function find($table, $where = '1=1', $arrCol = "*")
{
    $rlData = null;
    if($table != null){
        $table = trim(strip_tags($table));
        $sqlQuery = "SELECT ".$arrCol." FROM ".$table." WHERE 1=1 and ".$where;
        $query = query($sqlQuery);
        $rlData = array();
        if($query != null) {
            while($data = mysql_fetch_array($query)) {
                $rlData[] = $data;
            }
        }

    }
    return $rlData;
}

global $conn, $config;
$conn = getConnect($config);
